package com.itheima.xmy;


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class MainApp<mathTest1> {

    public static void main(String[] args) {
        Login();
    }

    //初始化集合
    static ArrayList<MathTest> mathTests = new ArrayList<>();
    MathTest mathTest1 = new AddTest(4, 5);
    MathTest mathTest2 = new DivideTest(5, 1);
    MathTest mathTest3 = new MultiplyTest(4, 3);
    MathTest mathTest4 = new SubtractTest(10, 5);

//        mathTests.add(mathTest1);
//        mathTests.add(mathTest2);
//        mathTests.add(mathTest3);
//        mathTests.add(mathTest4);

    //登陆系统
    public static void Login() {

        System.out.println("欢迎进入黑马计算系统");

        while (true) {
            User student = new Student("哪吒", "123456");
            User teacher = new Teacher("太乙真人", "123456");
            System.out.println("请输入您的用户名：");
            Scanner sc = new Scanner(System.in);
            String userInput = sc.next();
            System.out.println("请输入密码：");
            String userInputPassWord = sc.next();
            if (userInput.equals(student.getName())) {
                if (userInputPassWord.equals(student.getPassWord())) {
                    //进入学生系统
                    showStudentUI(student, mathTests);
                    break;
                }
            } else if (userInput.equals(teacher.getName())) {
                if (userInputPassWord.equals(teacher.getPassWord())) {
                    //进入老师系统
                    showTeacherUI(teacher, mathTests);
                    break;
                }
            } else {
                System.out.println("您输入的用户名或密码有误，请重新输入！");
            }
        }
    }

    //学生UI页面
    public static void showStudentUI(User user, ArrayList<MathTest> mathTests) {
        System.out.println("欢迎" + user.getName() + "学生进入系统");
        System.out.println("请开始您的答题：\n" +
                "您好，一共4道题目，请开始作答，答对一题得1分，答错不给分\n");


        while (true) {
            int score = answer(mathTests);
            if (score == 4) {
                System.out.println("最终得分：" + score);
                System.out.println("哇哦！算术这方面，你就是王者！佩服你");
                break;
            } else {
                System.out.println("最终得分：   " + score);
                System.out.println("成绩不服，输入Y继续挑战，否则输入其它退出！");
                Scanner scanner = new Scanner(System.in);
                String inPut = scanner.next();
                if (inPut.equals("Y")) {
                } else {
                    return;
                }
            }
        }
    }

    //答题方法
    public static int answer(ArrayList<MathTest> mathTests) {
        //定义一个积分器，用于返回成绩分数
        int counts = 0;
        //产生随机数，选择题号，共循环四次
        Random r = new Random();
        //标记

        int indext = 5;
        int[] arr = {10, 10, 10, 10};
        for (int i = 0; i < 4; ) {
            boolean flag = true;
            indext = r.nextInt(mathTests.size());
            for (int i1 = 0; i1 < 4; i1++) {
                if (indext == arr[i1]) {
                    flag = false;
                }
            }
            if (flag) {
                arr[i] = indext;
                i++;
            }
            //避免重复
        }
        for (int i = 0; i < 4; i++) {
            System.out.println("请回答以下问题，机会只有一次：");
            System.out.println(mathTests.get(arr[i]).getQuestion());
            Scanner scanner = new Scanner(System.in);
            int answer = scanner.nextInt();
            //判断mathTest的真实子类型
            if (mathTests.get(arr[i]) instanceof DivideTest) {
                if (answer == ((mathTests.get(arr[i]).getA() /
                        (mathTests.get(arr[i])).getB()))) {
                    System.out.println("回答正确！");
                    counts++;
                } else {
                    System.out.println("回答错误！");
                }
            } else if (mathTests.get(arr[i]) instanceof AddTest) {
                if (answer == ((mathTests.get(arr[i]).getA() +
                        (mathTests.get(arr[i])).getB()))) {
                    System.out.println("回答正确！");
                    counts++;
                } else {
                    System.out.println("回答错误！");
                }
            } else if (mathTests.get(arr[i]) instanceof MultiplyTest) {
                if (answer == ((mathTests.get(arr[i]).getA() *
                        (mathTests.get(arr[i])).getB()))) {
                    System.out.println("回答正确！");
                    counts++;
                } else {
                    System.out.println("回答错误！");
                }
            } else if (mathTests.get(arr[i]) instanceof SubtractTest) {
                if (answer == ((mathTests.get(arr[i]).getA() -
                        (mathTests.get(arr[i])).getB()))) {
                    System.out.println("回答正确！");
                    counts++;
                } else {
                    System.out.println("回答错误！");
                }
            }
        }
        return counts;
    }


    //老师UI页面
    public static void showTeacherUI(User user, ArrayList<MathTest> mathTests) {
        System.out.println("欢迎" + user.getName() + "老师进入系统");
        System.out.println("当前题库如下:");
        showTest(mathTests);
        System.out.println("是否要编辑题目,需要编辑输入Y,输入其他无需修改，并返回登录页面！\n" +
                "Y");
        Scanner sc = new Scanner(System.in);
        String inPut = sc.next();
        if (inPut.equals("Y")) {
            System.out.println("请选择您需要的操作：");
            System.out.println("1、添加题目");
            System.out.println("2、删除题目");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    addTest(mathTests);
                    break;
                case "2":
                    deleteTest(mathTests);
                    break;
            }

        } else {
            Login();
        }
    }


    //添加试题页面
    public static void addTest(ArrayList<MathTest> mathTests) {

        System.out.println("请选择需要添加的题型：");
        System.out.println("1：加法题");
        System.out.println("2：减法题");
        System.out.println("3：乘法题");
        System.out.println("4：除法题");
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        //选择需要修改的题型
        switch (s) {
            case "1":
                Scanner scanner1 = new Scanner(System.in);
                System.out.println("请输入第一个加数");
                int a = scanner1.nextInt();
                System.out.println("请输入第二个加数");
                int b = scanner1.nextInt();
                MathTest addTest = new AddTest(a, b);
                mathTests.add(addTest);
                System.out.println("题目：" + addTest.getQuestion() + " 添加成功！");
                break;
            case "2":
                Scanner scanner2 = new Scanner(System.in);
                System.out.println("请输入被减数");
                int m = scanner2.nextInt();
                System.out.println("请输入减数");
                int n = scanner2.nextInt();
                MathTest subtractTest = new SubtractTest(m, n);
                mathTests.add(subtractTest);
                System.out.println("题目：" + subtractTest.getQuestion() + " 添加成功！");
                break;
            case "3":
                Scanner scanner3 = new Scanner(System.in);
                System.out.println("请输入第一个乘数");
                int i = scanner3.nextInt();
                System.out.println("请输入第二个乘数");
                int j = scanner3.nextInt();
                MathTest multiplyTest = new MultiplyTest(i, j);
                mathTests.add(multiplyTest);
                System.out.println("题目：" + multiplyTest.getQuestion() + " 添加成功！");
                break;
            case "4":
                Scanner scanner4 = new Scanner(System.in);
                System.out.println("请输入被除数");
                int x = scanner4.nextInt();
                int y = 0;
                while (true) {
                    System.out.println("请输入除数");
                    y = scanner4.nextInt();
                    if (y == 0) {
                        System.out.println("您输入的除数有误，请重新输入");
                    } else {
                        break;
                    }
                }
                MathTest divedeTest = new DivideTest(x, y);
                mathTests.add(divedeTest);
                System.out.println("题目：" + divedeTest.getQuestion() + " 添加成功！");
                break;
            default:
                System.out.println("您的输入有误");
        }
        System.out.println("是否需要继续添加，请按“Y”键确认，否则按任意键返回登录页面：");
        String inPut = sc.next();
        if (inPut.equals("Y")) {
            addTest(mathTests);
        } else {
            showTest(mathTests);
            Login();
        }

    }

    //删除试题页面
    public static void deleteTest(ArrayList<MathTest> mathTests) {
        while (true) {
            System.out.println("请选择您需要删除的题号：");
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();

            if (num <= mathTests.size() && num > 0) {
                System.out.println(mathTests.get(num - 1).getQuestion() + "该题已被成功删除！");
                mathTests.remove(num - 1);
                System.out.println("当前题库如下：");
                showTest(mathTests);
//                for (int i = 0; i < mathTests.size(); i++) {
//                    System.out.print(i + 1 + "、");
//                    System.out.println(mathTests.get(i).getQuestion());
//                    if (mathTests.get(i) instanceof DivideTest) {
//                        System.out.println(mathTests.get(i).getA() + "➗" + mathTests.get(i).getB()
//                                + "=" + mathTests.get(i).getA() / mathTests.get(i).getB());
//                    }else  if (mathTests.get(i) instanceof AddTest) {
//                        System.out.println(mathTests.get(i).getA() + "+" + mathTests.get(i).getB()
//                                + "=" + (mathTests.get(i).getA() + mathTests.get(i).getB()));
//                    }else  if (mathTests.get(i) instanceof MultiplyTest) {
//                        System.out.println(mathTests.get(i).getA() + "*" + mathTests.get(i).getB()
//                                + "=" + (mathTests.get(i).getA() * mathTests.get(i).getB()));
//                    }else  if (mathTests.get(i) instanceof SubtractTest) {
//                        System.out.println(mathTests.get(i).getA() + "-" + mathTests.get(i).getB()
//                                + "=" + (mathTests.get(i).getA() - mathTests.get(i).getB()));
//                    }
//                }
                System.out.println("输入Y键继续删除题目，否则返回登录页面：");
                System.out.println("是否需要继续添加，请按“Y”键确认，否则按任意键返回登录页面");
                String inPut = sc.next();
                if (inPut.equals("Y")) {
                    deleteTest(mathTests);
                } else {
                    Login();
                }
            } else {
                System.out.println("您输入的题号有误，请重新输入:");
            }
        }
    }

    //展示题库
    public static void showTest(ArrayList<MathTest> mathTests) {
        for (int i = 0; i < mathTests.size(); i++) {
            System.out.print(i + 1 + "、");
            System.out.println(mathTests.get(i).getQuestion());
            System.out.print("参考答案：");
            if (mathTests.get(i) instanceof DivideTest) {
                System.out.println(mathTests.get(i).getA() + "➗" + mathTests.get(i).getB()
                        + "=" + mathTests.get(i).getA() / mathTests.get(i).getB());
            } else if (mathTests.get(i) instanceof AddTest) {
                System.out.println(mathTests.get(i).getA() + "+" + mathTests.get(i).getB()
                        + "=" + (mathTests.get(i).getA() + mathTests.get(i).getB()));
            } else if (mathTests.get(i) instanceof MultiplyTest) {
                System.out.println(mathTests.get(i).getA() + "*" + mathTests.get(i).getB()
                        + "=" + (mathTests.get(i).getA() * mathTests.get(i).getB()));
            } else if (mathTests.get(i) instanceof SubtractTest) {
                System.out.println(mathTests.get(i).getA() + "-" + mathTests.get(i).getB()
                        + "=" + (mathTests.get(i).getA() - mathTests.get(i).getB()));
            }
        }
    }
}

